from ui_pageTest1 import Ui_pageTest1
from ui_pageTest2 import Ui_pageTest2
from ui_pageTest3 import Ui_pageTest3
from ui_pageTest4 import Ui_pageTest4
from ui_JobCfgItem import Ui_JobCfgItem

from PyQt5 import QtWidgets, QtGui, QtCore

class JobCfgItem(QtWidgets.QWidget, Ui_JobCfgItem):
    def __init__(self, parent=None, index=0, keyBoard_callback=None):
        super(JobCfgItem, self).__init__(parent)
        self.setupUi(self)
        self.labIndex.setText(str(index))
        self.temp = float(self.TextTemp.text())
        self.isProbe1Enalbed = self.enableProbe1.isChecked()
        self.isProbe2Enalbed = self.enableProbe2.isChecked()
        SH=int(self.TextH.text()) * 3600
        SM=int(self.TextM.text()) * 60
        SS=int(self.TextS.text())
        self.keepTime=SH+SM+SS
        self.TextTemp.textChanged.connect(self.tempUpdated)
        self.TextH.textChanged.connect(self.timeUpdated)
        self.TextM.textChanged.connect(self.timeUpdated)
        self.TextS.textChanged.connect(self.timeUpdated)
        self.enableProbe1.clicked.connect(self.probeUpdated)
        self.enableProbe2.clicked.connect(self.probeUpdated)
        self.TextH.setVisible(False)

        if keyBoard_callback != None:
           self.TextM.mousePressEvent = lambda e: keyBoard_callback(self.TextM, e)
           self.TextS.mousePressEvent = lambda e: keyBoard_callback(self.TextS, e)
           self.TextTemp.mousePressEvent = lambda e: keyBoard_callback(self.TextTemp, e)

    def changeIndex(self, index):
      self.labIndex.setText(str(index))

      if index % 2 == 0:
         self.setBackgroundBlue()
      else:
         self.setBackgroundWhite()

    def tempUpdated(self):
      self.temp = float(self.TextTemp.text())

    def timeUpdated(self):
        SH=int(self.TextH.text()) * 3600
        SM=int(self.TextM.text()) * 60
        SS=int(self.TextS.text())
        self.keepTime=SH+SM+SS

        if self.keepTime > 3600:
           self.keepTime = 3600

    def probeUpdated(self):
        self.isProbe1Enalbed = self.enableProbe1.isChecked()
        self.isProbe2Enalbed = self.enableProbe2.isChecked()

    def setBackgroundBlue(self):
       pixMap = QtGui.QPixmap("./images/lightblue.png")
       pixMap.scaled(self.size(), QtCore.Qt.IgnoreAspectRatio)
       pal = QtGui.QPalette()
       pal.setBrush(QtGui.QPalette.Background, QtGui.QBrush(pixMap))

       self.setPalette(pal)

    def setBackgroundWhite(self):
       pixMap = QtGui.QPixmap("./images/white.png")
       pixMap.scaled(self.size(), QtCore.Qt.IgnoreAspectRatio)
       pal = QtGui.QPalette()
       pal.setBrush(QtGui.QPalette.Background, QtGui.QBrush(pixMap))

       self.setPalette(pal)



class NewTestPage1(QtWidgets.QWidget, Ui_pageTest1):
    def __init__(self, parent=None):
        super(NewTestPage1, self).__init__(parent)
        self.setupUi(self)


class NewTestPage2(QtWidgets.QWidget, Ui_pageTest2):
    def __init__(self, parent=None):
        super(NewTestPage2, self).__init__(parent)
        self.setupUi(self)
        confg_items=["无样品", "试样样品", "阳性对照", "阴性对照", "空白对照"]
        self.configSample1.addItems(confg_items)
        self.configSample1.setCurrentIndex(1)
        self.configSample2.addItems(confg_items)
        self.configSample2.setCurrentIndex(1)
        self.configSample3.addItems(confg_items)
        self.configSample3.setCurrentIndex(1)
        self.configSample4.addItems(confg_items)
        self.configSample4.setCurrentIndex(1)
        self.configSample5.addItems(confg_items)
        self.configSample5.setCurrentIndex(1)
        self.configSample6.addItems(confg_items)
        self.configSample6.setCurrentIndex(1)
        self.configSample7.addItems(confg_items)
        self.configSample7.setCurrentIndex(1)
        self.configSample8.addItems(confg_items)
        self.configSample8.setCurrentIndex(1)


class NewTestPage3(QtWidgets.QWidget, Ui_pageTest3):
    def __init__(self, parent=None, keyBoard_callback=None):
        super(NewTestPage3, self).__init__(parent)
        self.setupUi(self)
        self.items=[]
        self.input_cb = keyBoard_callback
        self.item1 = JobCfgItem(self.centerWidget, index=0, keyBoard_callback = self.input_cb)
        self.items.append(self.item1)
        index=0
        for item in self.items:
           item.move(0, index*50)
           index = index + 1
        self.add0.clicked.connect(lambda: self.insertLine(0))
        self.add1.clicked.connect(lambda: self.insertLine(1))
        self.add2.clicked.connect(lambda: self.insertLine(2))
        self.add3.clicked.connect(lambda: self.insertLine(3))
        self.add4.clicked.connect(lambda: self.insertLine(4))
        self.add5.clicked.connect(lambda: self.insertLine(5))
        self.add6.clicked.connect(lambda: self.insertLine(6))
        self.add7.clicked.connect(lambda: self.insertLine(7))
        self.del0.clicked.connect(lambda: self.removeLine(0))
        self.del1.clicked.connect(lambda: self.removeLine(1))
        self.del2.clicked.connect(lambda: self.removeLine(2))
        self.del3.clicked.connect(lambda: self.removeLine(3))
        self.del4.clicked.connect(lambda: self.removeLine(4))
        self.del5.clicked.connect(lambda: self.removeLine(5))
        self.del6.clicked.connect(lambda: self.removeLine(6))
        self.del7.clicked.connect(lambda: self.removeLine(7))

    def insertLine(self, index):
       if len(self.items) == 8:
          return
       newItem=JobCfgItem(self.centerWidget, index=index, keyBoard_callback=self.input_cb)
       self.items.insert(index, newItem)
       localIndex=0
       for item in self.items:
           item.changeIndex(localIndex)
           item.move(0, localIndex*50)
           item.show()
           localIndex = localIndex + 1

    def setItem(self, index, temp, enable1, enable2, time=60):
       if index == 0:
          item = self.items[0]
       else:
          item = JobCfgItem(self.centerWidget, index=1, keyBoard_callback=self.input_cb)
          self.items.append(item)
       item.TextTemp.setText(temp)
       item.enableProbe1.setChecked(enable1)
       item.enableProbe2.setChecked(enable2)
       item.isProbe1Enalbed = enable1
       item.isProbe2Enalbed = enable2

       timeM, timeS = divmod(time, 60)
       timeH, timeM = divmod(timeM, 60)

       print(type(time), time, timeH, timeM, timeS)

       item.TextH.setText(str(timeH))
       item.TextM.setText(str(timeM))
       item.TextS.setText(str(timeS))

       localIndex=0
       for item in self.items:
           item.changeIndex(localIndex)
           item.move(0, localIndex*50)
           item.show()
           localIndex = localIndex + 1

    def removeLine(self, index):
       print("index=%d, len=%d"%(index, len(self.items)))
       if index >= len(self.items) or len(self.items) == 1:
          return
       itemToBeRemoved = self.items[index]
       self.items.remove(itemToBeRemoved)
       itemToBeRemoved.deleteLater()
       localIndex=0
       for item in self.items:
           item.changeIndex(localIndex)
           item.move(0, localIndex*50)
           item.show()
           localIndex = localIndex + 1


class NewTestPage4(QtWidgets.QWidget, Ui_pageTest4):
    def __init__(self, parent=None):
        super(NewTestPage4, self).__init__(parent)
        self.setupUi(self)

class pageNewTest(QtWidgets.QWidget):
   def __init__(self, keyBoard_callback = None):
      super().__init__()

      self.btnNext = QtWidgets.QPushButton("下一页")
      self.btnNext.setFixedSize(150, 60)
      self.btnNext.clicked.connect(self.switchToNextPage)
      self.btnStart = QtWidgets.QPushButton("开始测试")
      self.btnStart.setFixedSize(150, 60)
      self.btnStartNSave = QtWidgets.QPushButton("保存并开始")
      self.btnStartNSave.setFixedSize(150, 60)
      self.btnStartNSave.setVisible(False)
      self.btnStart.setVisible(False)
      self.btnPrev = QtWidgets.QPushButton("上一页")
      self.btnPrev.setFixedSize(150, 60)
      self.btnPrev.setVisible(False)
      self.btnPrev.clicked.connect(self.switchToPrevPage)

      self.right_area = QtWidgets.QVBoxLayout()
      self.right_area.addSpacing(30)
      self.right_area.addWidget(self.btnPrev)
      self.right_area.addStretch()
      self.right_area.addWidget(self.btnStartNSave)
      self.right_area.addWidget(self.btnStart)
      self.right_area.addWidget(self.btnNext)
      self.right_area.addSpacing(30)

      self.testPage1 = NewTestPage1()
      self.testPage2 = NewTestPage2()
      self.testPage3 = NewTestPage3(parent=self, keyBoard_callback = keyBoard_callback)
      self.testPage4 = NewTestPage4()

      self.left_area = QtWidgets.QStackedWidget()
      self.left_area.addWidget(self.testPage1)
      self.left_area.addWidget(self.testPage2)
      self.left_area.addWidget(self.testPage3)
      self.left_area.addWidget(self.testPage4)

      layout_test_page = QtWidgets.QHBoxLayout()
      layout_test_page.addWidget(self.left_area)
      layout_test_page.addLayout(self.right_area)

      self.setLayout(layout_test_page)

      self.setFont(QtGui.QFont('times', 14, QtGui.QFont.Black))

   def switchToNextPage(self):
      curIndex = self.left_area.currentIndex()
      if curIndex != 3:
         curIndex = curIndex + 1
         self.left_area.setCurrentIndex(curIndex)

      self.btnPrev.setVisible(True)

      if curIndex == 3:
         self.btnStart.setVisible(True)
         self.btnStartNSave.setVisible(True)
         self.btnNext.setVisible(False)


   def switchToPrevPage(self):
      curIndex = self.left_area.currentIndex()
      if curIndex != 0:
         curIndex = curIndex - 1
         self.left_area.setCurrentIndex(curIndex)

      self.btnNext.setVisible(True)
      self.btnStart.setVisible(False)
      self.btnStartNSave.setVisible(False)

      if curIndex == 0:
         self.btnPrev.setVisible(False)


   def setStartTestCallBack(self, function):
      self.btnStart.clicked.connect(function)